import { save , update } from '../../../api/customer'

Page({
    data: {
        showLegalPerson1:false,
        showLegalPerson2:false,
        showAccountLicense:false,
        showOther: false,
        showOpenBankAccount: false,
        showBusiness: false,
        showOilSell: false,
        showLegalGrant: false,

        name: '',
        type: '',
        nature: '',
        phone: '',
        linkman: '',
        group_id: '',
        group_name: '',
        status: '0',
        address: '',
        meta: {
            license: '',
            wholesale: '',
            chemical: '',
            other: '',
            licenseCode:'',
            legalPerson1: "",
            legalPerson2: "",
            accountLicense:'',
            banknum:'',
            bankname:'',
            legalGrant:''
        },
        nextStepFlag: true,
        //新增商品进入传参
        sellearAdd: false,
        //修改客户进入
        sellearEdit: false,
        id:'',
    },
    onLoad: function (options) {
        this.data.name = options.name
        this.data.type = options.type
        this.data.nature = options.nature
        this.data.phone = options.phone
        this.data.linkman = options.linkman
        this.data.group_id = options.group_id
        this.data.group_name = options.group_name
        this.data.address = options.address
        this.data.id = options.id;
        //新增商品进入传参
        this.data.sellearAdd = options.sellearAdd
        this.data.sellearEdit = options.sellearEdit;
        if (this.data.sellearEdit != 'undefined') {
            this.data.meta.license = options.license;
            this.data.meta.chemical = options.chemical;
            this.data.meta.wholesale = options.wholesale;
            this.data.meta.licenseCode = options.licenseCode
            this.data.meta.other = options.other;
            
            this.data.meta.legalPersonCode = options.legalPersonCode;
            this.data.meta.legalPerson1 = options.legalPerson1;
            this.data.meta.legalPerson2 = options.legalPerson2;
            this.data.meta.accountLicense = options.accountLicense;
            this.data.meta.banknum = options.banknum;
            this.data.meta.bankname = options.bankname;
            this.data.meta.legalGrant = options.legalGrant;

            
        }
        this.setData({
            name: this.data.name,
            type: this.data.type,
            nature: this.data.nature,
            phone: this.data.phone,
            linkman: this.data.linkman,
            group_id: this.data.group_id,
            group_name: this.data.group_name,
            meta: this.data.meta,
            sellearEdit:this.data.sellearEdit,
        })
    },
    banknumChange(e){
        this.data.meta.banknum  =  e.detail.value
        this.setData({
            meta:this.data.meta
        })
    },
    businessNoChange(e){
        this.data.meta.licenseCode  =  e.detail.value
        this.setData({
            meta:this.data.meta
        })
    },
    banknameChange(e){
        this.data.meta.bankname  =  e.detail.value
        this.setData({
            meta:this.data.meta
        })
    },
    legalPersonCodeChange(e){
        this.data.meta.legalPersonCode  =  e.detail.value
        this.setData({
            meta:this.data.meta
        })
    },
    businessChange(e) {
        this.data.meta.license = ""
        e.detail.images.forEach(item => {
            if (this.data.meta.license == "") {
                this.data.meta.license = item
            } else {
                this.data.meta.license = this.data.meta.license + "," + item
            }
        })
        this.setData({
            meta: this.data.meta
        })
    },
    legalPerson1Change(e) {
        this.data.meta.legalPerson1 = ""
        e.detail.images.forEach(item => {
            if (this.data.meta.legalPerson1 == "") {
                this.data.meta.legalPerson1 = item
            } else {
                this.data.meta.legalPerson1 = this.data.meta.legalPerson1 + "," + item
            }
        })
        this.setData({
            meta: this.data.meta
        })
    },
    legalPerson2Change(e) {
        this.data.meta.legalPerson2 = ""
        e.detail.images.forEach(item => {
            if (this.data.meta.legalPerson2 == "") {
                this.data.meta.legalPerson2 = item
            } else {
                this.data.meta.legalPerson2 = this.data.meta.legalPerson2 + "," + item
            }
        })
        this.setData({
            meta: this.data.meta
        })
    },
    legalGrantChange(e) {
        this.data.meta.legalGrant = ""
        e.detail.images.forEach(item => {
            if (this.data.meta.legalGrant == "") {
                this.data.meta.legalGrant = item
            } else {
                this.data.meta.legalGrant = this.data.meta.legalGrant + "," + item
            }
        })
        this.setData({
            meta: this.data.meta
        })
    },
    accountLicenseChange(e){
        this.data.meta.accountLicense = ""
        e.detail.images.forEach(item => {
            if (this.data.meta.accountLicense == "") {
                this.data.meta.accountLicense = item
            } else {
                this.data.meta.accountLicense = this.data.meta.accountLicense + "," + item
            }
        })
        this.setData({
            meta: this.data.meta
        })
    },
    wholesaleChange(e) {
        this.data.meta.wholesale = ""
        e.detail.images.forEach(item => {
            if (this.data.meta.wholesale == "") {
                this.data.meta.wholesale = item
            } else {
                this.data.meta.wholesale = this.data.meta.wholesale + "," + item
            }
        })
        this.setData({
            meta: this.data.meta
        })
    },
    ApiModelPropertyChange(e) {
        this.data.meta.chemical = ""
        e.detail.images.forEach(item => {
            if (this.data.meta.chemical == "") {
                this.data.meta.chemical = item
            } else {
                this.data.meta.chemical = this.data.meta.chemical + "," + item
            }
        })
        this.setData({
            meta: this.data.meta
        })
    },
    otherChange(e) {
        this.data.meta.other = ""
        e.detail.images.forEach(item => {
            if (this.data.meta.other == "") {
                this.data.meta.other = item
            } else {
                this.data.meta.other = this.data.meta.other + "," + item
            }
        })
        this.setData({
            meta: this.data.meta
        })
    },
    showOne() {
        this.data.showBusiness = !this.data.showBusiness
        this.setData({
            showBusiness: this.data.showBusiness
        })
    },
    showLegalPerson1() {
        this.data.showLegalPerson1 = !this.data.showLegalPerson1
        this.setData({
            showLegalPerson1: this.data.showLegalPerson1
        })
    },
    showLegalPerson2() {
        this.data.showLegalPerson2 = !this.data.showLegalPerson2
        this.setData({
            showLegalPerson2: this.data.showLegalPerson2
        })
    },
    showAccountLicense() {
        this.data.showAccountLicense = !this.data.showAccountLicense
        this.setData({
            showAccountLicense: this.data.showAccountLicense
        })
    },
    showTwo() {
        this.data.showOpenBankAccount = !this.data.showOpenBankAccount
        this.setData({
            showOpenBankAccount: this.data.showOpenBankAccount
        })
    },
    showThree() {
        this.data.showOilSell = !this.data.showOilSell
        this.setData({
            showOilSell: this.data.showOilSell
        })
    },
    showFour() {
        this.data.showOther = !this.data.showOther
        this.setData({
            showOther: this.data.showOther
        })
    },

    nextStep() {
        if (this.data.meta.license == "")  {
            wx.showToast({
                icon: "none",
                title: '请上传营业执照',
            })
            return false
        }
        if (this.data.meta.licenseCode == "") {
            wx.showToast({
                icon: "none",
                title: '请输入证件号',
            })
            return false
        }
        if (this.data.meta.licenseCode.length != '15' && this.data.meta.licenseCode.length != '18') {
            wx.showToast({
                icon: "none",
                title: '请输入正确证件号',
            })
            return false
        }
        if (this.data.meta.wholesale == "" && this.data.meta.chemical == "") {
            wx.showToast({
                icon: "none",
                title: '成品油和危险化学品二选一,请上传',
            })
            return false
        }
        if (this.data.meta.legalPersonCode == "") {
            wx.showToast({
                icon: "none",
                title: '请输入身份证号',
            })
            return false
        }
        if (this.data.meta.legalPerson1 == "") {
            wx.showToast({
                icon: "none",
                title: '请上传身份证正面',
            })
            return false
        }
        if (this.data.meta.legalPerson2 == "") {
            wx.showToast({
                icon: "none",
                title: '请上传身份证反面',
            })
            return false
        }
        if (this.data.meta.legalGrant == "") {
            wx.showToast({
                icon: "none",
                title: '请上传法人授权书',
            })
            return false
        }
        
        if (this.data.meta.banknum == "") {
            wx.showToast({
                icon: "none",
                title: '请输入开户行',
            })
            return false
        }
        if (this.data.meta.bankname == "") {
            wx.showToast({
                icon: "none",
                title: '请输入开户行账号',
            })
            return false
        }
        if (this.data.meta.accountLicense == "") {
            wx.showToast({
                icon: "none",
                title: '请上传开户许可证',
            })
            return false
        }
        if (this.data.nextStepFlag == true) {
            this.data.nextStepFlag = false
            this.setData({
                nextStepFlag: this.data.nextStepFlag
            })
            let settime = setTimeout(() => {
                this.data.nextStepFlag = true
                this.setData({
                    nextStepFlag: this.data.nextStepFlag
                })
                clearTimeout(settime)
            }, 2000)
            let params = {
                "address": this.data.address,
                "linkman": this.data.linkman,
                "logo": this.data.logo,
                "meta": JSON.stringify(this.data.meta),
                "msg": this.data.msg,
                "name": this.data.name,
                "phone": this.data.phone,
                "status": this.data.status,
                "type": this.data.type,
                "userId": this.data.userId,
                "groupId": this.data.group_id == 'undefined' ? ""  : this.data.group_id,
                "nature": this.data.nature,
                'id':this.data.id
            }
            if(this.data.sellearEdit == 'true'){
                update(params).then(res => {
                    wx.showToast({
                        icon: "none",
                        title: '更新成功',
                    })
                    let setTime = setTimeout(() => {
                        if (this.data.sellearAdd) {
                            wx.navigateBack({
                                delta: 3
                            })
                        } else {
                            wx.switchTab({
                                url: '/pages/index/index',
                            })
                        }
                        clearTimeout(setTime)
                    }, 1000)
                })
                return false
            }

            save(params).then(res => {
                wx.showToast({
                    icon: "none",
                    title: '提交成功',
                })
                let setTime = setTimeout(() => {
                    if (this.data.sellearAdd) {
                        wx.navigateBack({
                            delta: 3
                        })
                    } else {
                        wx.switchTab({
                            url: '/pages/index/index',
                        })
                    }

                    clearTimeout(setTime)
                }, 1000)
            })
        }

    }
});
